﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

using RTW.Core.Datas;
using RTW.Models;
using RTW.Servers;
using RTW.Web.Comm;
using RTW.Web.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RTW.Web.Controllers
{
    public class HomeController : CommController
    {
        IWebHostEnvironment env;
        HomeMenuServer _menuServer;
        TMenuServer _tmenuServer;
        TLogServer _logServer;
        TMsgServer _msgServer;
        public HomeController(IWebHostEnvironment _env) { env = _env;
            _menuServer = new HomeMenuServer();
            _logServer = new TLogServer();
            _msgServer = new TMsgServer();
            _tmenuServer = new TMenuServer();
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult HomePage()
        {
            GetMenuHome();
            HomePageViewModel hpvm = new HomePageViewModel
            {
                user = base.UserInfo
            };
            DownloadViewModel dvm = new DownloadViewModel();
            BindDownloadInfo(dvm);
            hpvm.download = dvm;
            hpvm.msgs = _msgServer.GetMsgsByUser(UserID);
            hpvm.timelog = _logServer.GetLogsByUser(UserID);
            return View(hpvm);
        }

        private void BindDownloadInfo(DownloadViewModel dwm)
        {
            //下载插件分类路径定义
            string fileDir = env.WebRootPath + @"\download\";

            DirectoryInfo dir = new DirectoryInfo(fileDir + @"插件\");
            FileInfo[] dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                dwm.list.Add(f);
            }

            dir = new DirectoryInfo(fileDir + @"学习文档\");
            dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                dwm.list1.Add(f);
            }

            dir = new DirectoryInfo(fileDir + @"有关资料\");
            dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                dwm.list2.Add(f);
            }

            dir = new DirectoryInfo(fileDir + @"其他\");
            dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                if (f.Name != "方正小标宋简体.ttf")
                {
                    dwm.list3.Add(f);
                }
            }
        }


        /// <summary>
        /// 获取首页统计信息
        /// </summary>
        /// <returns></returns>
        public void GetMenuHome()
        {
            //string sql = string.Format("select * from HomeMenu where DMID='{0}'", UserDMID);
            //DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            IList<HomeMenu> homeMenus = _menuServer.Get(m => m.DMID == UserDMID);
            StringBuilder sb = new StringBuilder();
            //for (int i = 0; i < dt.Rows.Count; i++)
            foreach(HomeMenu menu in homeMenus)
            {
                //string Name = dt.Rows[i]["Name"].ToString();
                //string IsShow = dt.Rows[i]["IsShow"].ToString();
                //string PageUrl = dt.Rows[i]["PageUrl"].ToString();
                //string ImgPath = dt.Rows[i]["ImgPath"].ToString();

                string Name = menu.Name;
                string IsShow = menu.IsShow.ToString();
                string PageUrl = menu.PageUrl;
                string ImgPath = menu.ImgPath;
                if (IsShow == "0")
                {
                    if (!string.IsNullOrWhiteSpace(Name))
                    {
                      //  string sqlStr = "";
                        object obj = "";
                        switch (Name)
                        {
                            case "诉讼档案":
                               // sqlStr = string.Format("select count(ID) from TFiles where FileType=1 and  DMID='{0}'", UserDMID);//诉讼
                                obj = new BaseDAL<TFiles>().Count(m => m.FileType == 1 && m.DMID == UserDMID);
                                break;
                            case "传统文书":
                               // sqlStr = string.Format("select count(ID) from TFiles where FileType=2 and Flag1=1 and DMID='{0}'", UserDMID);//传统文书
                                obj = new BaseDAL<TFiles>().Count(m => m.FileType == 2 && m.DMID == UserDMID);
                                break;
                            case "简化文书":
                                //sqlStr = string.Format("select count(a.ID) from TFiles as a left join CaseValueSs as b on b.CaseId=(','+a.ID+',') where a.FileType=2 and a.Flag1=2 and  FType!='专题档案' and  a.DMID='{0}'", UserDMID);//简化文书
                                obj = new BaseDAL<TFiles>().Count(m => m.FileType == 2 && m.FType != "专题档案"&&m.DMID == UserDMID);
                                break;
                            case "基建档案":
                               // sqlStr = string.Format("select count(ID) from TCapitalCon where DMID='{0}' and IsDelete!=1;", UserDMID);   //基建档案
                                obj = new BaseDAL<TCapitalCon>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "工程项目":
                               // sqlStr = string.Format("select count(ID) from TEngDevice where DMID='{0}' and IsDelete!=1;", UserDMID);  //工程项目
                                obj = new BaseDAL<TEngDevice>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "设备档案":
                               // sqlStr = string.Format("select count(ID) from TEquipment where DMID='{0}' and IsDelete!=1 ;", UserDMID);//设备档案
                                obj = new BaseDAL<TEquipment>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "录音、录像带":
                                //sqlStr = string.Format("select count(ID) from TSound where DMID='{0}' and IsDelete!=1;", UserDMID);   //录音录像带
                                obj = new BaseDAL<TSound>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "照片档案"://照片
                                //sqlStr = string.Format("select count(ID) from TImage where DMID='{0}' and IsDelete!=1;", UserDMID);
                                obj = new BaseDAL<TImage>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "音视频":
                                //sqlStr = string.Format("select count(ID) from TAudioVideo where DMID='{0}' and IsDelete!=1;", UserDMID);   //音视频
                                obj = new BaseDAL<TAudioVideo>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "光盘":
                               // sqlStr = string.Format("select count(ID) from TElectronic where Bak1='光盘' and DMID='{0}' and IsDelete!=1;", UserDMID);  //光盘
                                obj = new BaseDAL<TElectronic>().Count(m => m.IsDelete != "1"&& m.Bak1== "光盘" && m.DMID == UserDMID);
                                break;
                            case "电子文件":
                               // sqlStr = string.Format("select count(ID) from TElectronic where Bak1='电子文件' and DMID='{0}' and IsDelete!=1;", UserDMID);  //电子文件
                                obj = new BaseDAL<TElectronic>().Count(m => m.IsDelete != "1" &&m.Bak1== "电子文件" && m.DMID == UserDMID);
                                break;
                            case "技术档案":
                               // sqlStr = string.Format("select count(ID) from TTechnology where DMID='{0}' and IsDelete!=1;", UserDMID);  //技术
                                obj = new BaseDAL<TTechnology>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "会计档案":
                               // sqlStr = string.Format("select count(ID) from TAccounting where DMID='{0}' and IsDelete!=1;", UserDMID);  //会计
                                obj = new BaseDAL<TAccounting>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "实物档案":
                               // sqlStr = string.Format("select count(ID) from TReal where DMID='{0}' and IsDelete!=1;", UserDMID);  //实物
                                obj = new BaseDAL<TReal>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "资料":
                                //sqlStr = string.Format("select count(ID) from TData where DMID='{0}' and IsDelete!=1;", UserDMID);   //资料
                                obj = new BaseDAL<TData>().Count(m => m.IsDelete != "1" && m.DMID == UserDMID);
                                break;
                            case "专题档案":
                                //sqlStr = string.Format("select count(ID) from TZtda where DMID='{0}';", UserDMID);//专题档案
                                obj = new BaseDAL<TZtda>().Count(m =>  m.DMID == UserDMID);
                                break;
                            case "发文汇集":
                                //sqlStr = string.Format("select count(ID) from TFiles where FileType=2 and Flag1=3 and DMID='{0}';", UserDMID);//发文汇集
                                obj = new BaseDAL<TFiles>().Count(m => m.FileType == 2 &&m.Flag1==3&& m.DMID == UserDMID);
                                break;
                        }

                       // object obj = DbHelperSQL.GetSingle(sqlStr.ToString());

                        sb.AppendFormat("<div class=\"col-sm-4\">");
                        // sb.AppendFormat("<div class=\"widget style1 {0}\" onclick=\"window.open('{1}', '{2}'); \">", GetMenuBgName(i), PageUrl, Name);
                        sb.AppendFormat("<div class=\"widget style1 zui-bg{0}\" onclick=\"window.open('{1}', '{2}'); \">", homeMenus.IndexOf(menu), PageUrl, Name);
                        sb.AppendFormat("<div class=\"row\">");
                        sb.AppendFormat("<div class=\"col-xs-4\">");
                        sb.AppendFormat("<i class=\"{0}\"></i>", ImgPath);
                        sb.AppendFormat("</div>");
                        sb.AppendFormat("<div class=\"col-xs-8 text-right\">");
                        sb.AppendFormat("<span>{0}</span>", Name);
                        sb.AppendFormat("<h2 class=\"font-bold\">{0}</h2>", obj != null ? obj.ToString() : "0");
                        sb.AppendFormat("</div>");
                        sb.AppendFormat("</div>");
                        sb.AppendFormat("</div>");
                        sb.AppendFormat("</div>");
                    }
                    else
                    {
                        sb.AppendFormat("");
                    }
                }
                else
                {
                    sb.AppendFormat("");
                }
            }
            //home.InnerHtml = sb.ToString();
            ViewBag.home= sb.ToString();
        }



        #region 加载菜单数据
        /// <summary>
        /// 加载菜单数据
        /// </summary>
        /// <returns></returns>
        public string LoadMenu()
        {
            StringBuilder str = new StringBuilder();
            var menulist = _tmenuServer.GetMenuTreeByUser(base.UserID);
            //if (menulist != null)
            //{
            //    foreach (var item in menulist)
            //    {
            //        var pid = item.id;
            //        str.Append("<li>");
            //        str.Append("<a href=\"javascript:;\">");
            //        str.Append("<i class=\"iconfont\">&#xe6b8;</i> <cite>" + item.sys_menuname + "</cite> <i class=\"iconfont nav_right\">&#xe697;</i>");
            //        str.Append("</a>");
            //        var childmenulist = db.sys_menu.SqlQuery("select top 100 * from sys_menu where sys_pid=" + pid + " and sys_state=0").ToList();
            //        if (childmenulist != null)
            //        {
            //            str.Append("<ul class=\"sub-menu\">");
            //            foreach (var submenu in childmenulist)
            //            {
            //                str.Append("<li>");
            //                str.Append("<a _href=\"/" + (submenu.sys_menuurl) + "\">");
            //                str.Append("<i class=\"iconfont\">&#xe6a7;</i> <cite>" + submenu.sys_menuname + "</cite>");
            //                str.Append("</a>");
            //                str.Append("</li>");
            //            }
            //            str.Append("</ul>");
            //        }
            //        str.Append("</li>");


            //    }
            //}
            return str.ToString();

        }
        #endregion
    }



}
